
#include <project.h>

#define COMMOBJ_CPP

/**/#define DEBUG_COMMOBJ/**/
#ifdef DEBUG_COMMOBJ
#define DEBUG_95
#endif

#include <CommObj.hpp>
#include <define.h>
#include <modem.h>
#include <msgfuncs.h>
#include <msg.h>
#include <modemmsg.h>
#include <spdebug.h>

CommObject CobCommunication;

CommObject::CommObject() {
    //can't think for moment what we need to do here but
    // a constructor is always useful!
    fLinkUp = VS_FALSE;
    timer = new CVerixCommTimer;
}

int CommObject::inBegin(void* oldCommObject,int CPTIndex,VS_BOOL usePrimaryLine) {
    short shRetVal;

    // non of parameters here are used, but for consistant API still used
    timer->SetTimer(30 * 1000);     // old base code hard codes this to 30 but we could get this from config

    if ((shRetVal = UCL_Obj->Open(timer)) <= UCL_SUCCESS)
        return VS_ERR;

    if ((shRetVal = UCL_Obj->Dial(timer)) != UCL_SUCCESS)
        return VS_ERR;

    return VS_SUCCESS;
}

int CommObject::inCheck(void* oldCommObject) {
    short shRetVal;

    timer->SetTimer(30 * 1000);     // old base code USES cpt SO WE COULD use shGetCarrierTimeOut
    if ((shRetVal = UCL_Obj->GetDialResponse(timer)) <= UCL_SUCCESS)
        return VS_ERR;

    fLinkUp = VS_TRUE;
    return VS_SUCCESS;
}

int CommObject::inSend(void* oldCommObject,char* data,int size,VS_BOOL confirmSend,int timeoutVal,long messageId) {
    int retVal;
    short sBytes;

    // confirm send does not exist so ignore it.
    if (messageId != 0L)
        vdDisplayMessageFromFile(messageId, STATUS_MSG);

    timer->SetTimer(timeoutVal);        
    retVal = UCL_Obj->Send((unsigned char *) data, (short) size, &sBytes, timer);

    switch (retVal) {
    case UCL_SUCCESS:
        retVal = (int) sBytes;
        break;
    case E_UCL_NO_CARRIER:
        fLinkUp = VS_FALSE;
        if (lnGGErrorMessage() == 0L)
            vdSetTranError(LOST_CARRIER_SEND_MSG21);
        retVal = VS_CARRIER;
        break;
    case E_UCL_TIMEOUT:
        fLinkUp = VS_FALSE;
        vdSetTranError(NO_HOST_RESPONSE);
        retVal = VS_TIMEOUT;
        break;
    default:
        retVal = VS_ERR;    // some other error;
        break;
    }

    return retVal;
}

int CommObject::inReceive(void* oldCommObject,char* data,int size,int timeoutVal,long messageId) {
    int retVal = VS_SUCCESS;
    short rBytes;

    if (messageId != 0L)
        vdDisplayMessageFromFile(messageId, STATUS_MSG);

    timer->SetTimer(timeoutVal);        
    if ((retVal = UCL_Obj->Receive((unsigned char *) data, (short) size, &rBytes, timer)) == UCL_SUCCESS)
        return ((int) rBytes);

    switch (retVal) {
    case UCL_SUCCESS:
        retVal = VS_SUCCESS;
        break;
    case E_UCL_NO_CARRIER:
        fLinkUp = VS_FALSE;
        if (lnGGErrorMessage() == 0L)
            vdSetTranError(LOST_CARRIER_SEND_MSG21);
        retVal = VS_CARRIER;
        break;
    case E_UCL_TIMEOUT:
        fLinkUp = VS_FALSE;
        vdSetTranError(NO_HOST_RESPONSE);
        retVal = VS_TIMEOUT;
        break;
    default:
        retVal = VS_ERR;    // some other error;
        break;
    }

    return retVal;
}

int CommObject::inFlush(void) {
    // no such functionality in UCL so lets ignore
    pdebug(("CommObject::inFlush"));
    return (VS_SUCCESS);
}

int CommObject::inEnd(void*) {
    short shRetVal;

    fLinkUp = VS_FALSE;
    timer->SetTimer(30 * 1000);     // Again this could be configurable timeout
    if ((shRetVal = UCL_Obj->Close(timer)) != UCL_SUCCESS)
        return VS_ERR;

    return VS_SUCCESS;
}

void CommObject::setUCL_Object(CUcl* UCL_Obj) {
    this->UCL_Obj = UCL_Obj;
}

CUcl* CommObject::getUCL_Object() {
    return UCL_Obj;
}

#undef COMMOBJ_CPP
